package com.lucker.controller.caipiaomisstype;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.lucker.common.model.MissTypeObj;
import com.lucker.controller.BaseController;
import com.lucker.ds.model.CaiPiaoMissType;
import com.lucker.framework.constant.ResponseBaseModel;
import com.lucker.framework.constant.ResponseResultModel;
import com.lucker.service.caipiaomisscodelist.CaiPiaoMissCodeListService;
import com.lucker.service.caipiaomisstype.CaiPiaoMissTypeService;

@Controller
@RequestMapping(value = "miss")
public class CaiPiaoMissTypeController extends BaseController {

	@Autowired
	CaiPiaoMissTypeService caiPiaoMissTypeService;
	
	@Autowired
	CaiPiaoMissCodeListService caiPiaoMissCodeListService;
	
	private ExecutorService	executorService	= Executors.newFixedThreadPool(10);
	
	@RequestMapping(value="/list",method = RequestMethod.POST)
	public @ResponseBody ResponseEntity<?> commit_opencodev1(HttpServletRequest req ,@RequestBody MissTypeObj missTypeObj) {
		try {
			if(missTypeObj == null) {
				return new ResponseEntity<>(new ResponseBaseModel(29002,"参数不能为空! "), HttpStatus.OK);
			}
			if(StringUtils.isBlank(missTypeObj.getMissTypeId())) {
				return new ResponseEntity<>(new ResponseBaseModel(29002,"参数 missTypeId 不能为空 ! "), HttpStatus.OK);
			}
			if(getRequestHeader(req) == null) {
				return new ResponseEntity<>(new ResponseBaseModel(29002,"公共参数不能为空! "), HttpStatus.OK);
			}
			if(StringUtils.isBlank(getRequestHeader(req).getCaiId())) {
				return new ResponseEntity<>(new ResponseBaseModel(29002,"公共参数 caiId 不能为空! "), HttpStatus.OK);
			}
			CaiPiaoMissType type = new CaiPiaoMissType();
			type.setMissTypeParentId(Long.valueOf(missTypeObj.getMissTypeId()));
			type.setCaiId(Long.valueOf(getRequestHeader(req).getCaiId()));
			List<CaiPiaoMissType> list = caiPiaoMissTypeService.queryAll(type);
			List<MissTypeObj> missTypeObjList = new ArrayList<MissTypeObj>();
			for(CaiPiaoMissType miss : list) {
				
				CaiPiaoMissType child = new CaiPiaoMissType();
				child.setMissTypeParentId(miss.getMissTypeId());
				child.setCaiId(Long.valueOf(getRequestHeader(req).getCaiId()));
				int childCount = caiPiaoMissTypeService.queryAllCount(child);
				
				MissTypeObj retMissTypeObj = new MissTypeObj();
				retMissTypeObj.setChildrenCount(String.valueOf(childCount));
				retMissTypeObj.setMaxCurrentMissValue(String.valueOf(miss.getMaxCurrentMissValue()));
				retMissTypeObj.setMissTypeId(String.valueOf(miss.getMissTypeId()));
				retMissTypeObj.setMissTypeName(miss.getMissTypeName());
				retMissTypeObj.setMissTypeParentId(String.valueOf(miss.getMissTypeParentId()));
				retMissTypeObj.setProbability(String.valueOf(miss.getProbability()));
				retMissTypeObj.setTheoryCycle(String.valueOf(miss.getTheoryCycle()));
				retMissTypeObj.setCaiId(String.valueOf(miss.getCaiId()));
				retMissTypeObj.setCaiTypeId(String.valueOf(miss.getCaiTypeId()));
				retMissTypeObj.setStstus(miss.getStatus());
				missTypeObjList.add(retMissTypeObj);
			}
			
			return new ResponseEntity<>(new ResponseResultModel<List<MissTypeObj>>(missTypeObjList),HttpStatus.OK);
		}catch(Exception e) {
			this.getLoger().error("提交异常 {} , {}", e.getMessage(),e);
			return new ResponseEntity<>(new ResponseBaseModel(29002,e.getMessage()), HttpStatus.OK);
		}
	}
	 
	
	@RequestMapping(value="/init_miss_type",method = RequestMethod.POST)
	public @ResponseBody ResponseEntity<?> init_miss_type(HttpServletRequest req) {
		try {
			
			executorService.execute(new Runnable() {
				@Override
				public void run() {
					try {
						caiPiaoMissTypeService.initMissTypeList();
					} catch (Exception e) {
						//getLoger().error("匹配中奖号码错误, 详情: {} " , e.getMessage() , e );
					}
				}
			});
			
			
			return new ResponseEntity<>(new ResponseResultModel<String>("OK"),HttpStatus.OK);
		}catch(Exception e) {
			this.getLoger().error("提交异常 {} , {}", e.getMessage(),e);
			return new ResponseEntity<>(new ResponseBaseModel(29002,e.getMessage()), HttpStatus.OK);
		}
	}
	
	@RequestMapping(value="/init_miss_type_codelist",method = RequestMethod.POST)
	public @ResponseBody ResponseEntity<?> init_miss_type_codelist(HttpServletRequest req,@RequestBody MissTypeObj missTypeObj) {
		try {
			
			if(missTypeObj == null) {
				return new ResponseEntity<>(new ResponseBaseModel(29002,"参数不能为空! "), HttpStatus.OK);
			}
			if(StringUtils.isBlank(missTypeObj.getCaiId())) {
				return new ResponseEntity<>(new ResponseBaseModel(29002,"参数 caiId 不能为空 ! "), HttpStatus.OK);
			}
			
			executorService.execute(new Runnable() {
				@Override
				public void run() {
					try {
						//int cids[] = {105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,201,202,203};
						//int cids[] = {105};
						int cids[] = {101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,201,202,203,123,124};
						for(int i : cids) {
							caiPiaoMissCodeListService.initCaiPiaoMissCodeList(Long.valueOf(i));
						}
						
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
			
			
			return new ResponseEntity<>(new ResponseResultModel<String>("OK"),HttpStatus.OK);
		}catch(Exception e) {
			this.getLoger().error("提交异常 {} , {}", e.getMessage(),e);
			return new ResponseEntity<>(new ResponseBaseModel(29002,e.getMessage()), HttpStatus.OK);
		}
	}
	
	
	
	
	@RequestMapping(value="/miss_type_codelist",method = RequestMethod.POST)
	public @ResponseBody ResponseEntity<?> miss_type_codelist(HttpServletRequest req,@RequestBody JSONObject missTypeObj) {
		try {
			if(getRequestHeader(req) == null) {
				return new ResponseEntity<>(new ResponseBaseModel(29002,"公共参数不能为空! "), HttpStatus.OK);
			}
			
			if(StringUtils.isBlank(getRequestHeader(req).getCaiId())) {
				return new ResponseEntity<>(new ResponseBaseModel(29002,"公共参数 caiId 不能为空! "), HttpStatus.OK);
			}
			
			if(missTypeObj == null) {
				return new ResponseEntity<>(new ResponseBaseModel(29002,"参数不能为空! "), HttpStatus.OK);
			}

			if(StringUtils.isBlank(missTypeObj.getString("missTypeId"))) {
				return new ResponseEntity<>(new ResponseBaseModel(29002,"参数 missTypeId 不能为空 ! "), HttpStatus.OK);
			}
			
			return caiPiaoMissCodeListService.missCodeList(Long.valueOf(getRequestHeader(req).getCaiId()) , Long.valueOf(missTypeObj.getString("missTypeId")));
		}catch(Exception e) {
			this.getLoger().error("提交异常 {} , {}", e.getMessage(),e);
			return new ResponseEntity<>(new ResponseBaseModel(29002,e.getMessage()), HttpStatus.OK);
		}
	}
	
	
	
}
